import { createStore } from 'vuex'

export interface ResponseType<P> {
  code: number;
  msg: string;
  data: P;
}

export interface ImageProps {
  _id?: string
  url?: string
  createdAt?: string
  fitUrl?: string
}
export interface ColumnProps {
  _id: string
  title: string
  avatar?: ImageProps
  description: string
}

export interface UserProps {
  isLogin: boolean
  nickName?: string
  _id?: string
  column?: string
  email?: string
  avatar?: ImageProps
  description?: string
}

export interface PostProps {
  _id?: string
  title: string
  excerpt?: string
  content?: string
  image?: ImageProps | string
  createdAt?: string
  column: string
  author?: string | UserProps
  isHTML?: boolean
}

interface ListProps<P> {
  [id: string]: P
}

export interface GlobalErrorProps {
  status: boolean
  message?: string
}
export interface GlobalDataProps {
  error: GlobalErrorProps
  token: string
  loading: boolean
  columns: {
    data: ListProps<ColumnProps>
    currentPage: number
    total: number
  }
  posts: {
    data: ListProps<PostProps>
    loadedColumns: string[]
  }
  user: UserProps
}

const store = createStore<GlobalDataProps>({
  state: {
    token: localStorage.getItem('token') || '',
    loading: false,
    columns: {
      data: {},
      currentPage: 0,
      total: 0
    },
    posts: { data: {}, loadedColumns: [] },
    user: {
      isLogin: false
    },
    error: {
      status: false
    }
  },
  mutations: {
  },
  actions: {
  },
  getters: {
  },
  modules: {
  }
})

export default store
